﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq; // 使用 JArray 和 JObject
using System.IO;
using System.Windows;

namespace StoreTools.Tools
{
    public static class ParseJson
    {
        public static JObject ReadJObject(string filePath)
        {
            if (!File.Exists(filePath))
            {
                MessageBox.Show($"文件路径错误【{filePath}】");
                return null;
            }
            try
            {
                string json = File.ReadAllText(filePath);
                JObject objs = JObject.Parse(json);
                return objs;
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.StackTrace);
                return null;
            }
        }

        public static JArray ReadJArray(string filePath)
        {
            if (!File.Exists(filePath))
            {
                MessageBox.Show($"文件路径错误【{filePath}】");
                return null;
            }
            try
            {
                string json = File.ReadAllText(filePath);
                JArray arjs = JArray.Parse(json);
                return arjs;
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.StackTrace);
                return null;
            }
            
        }

        public static void WriteJObject(string filePath, JContainer jsobj) 
        {
            if (!File.Exists(filePath))
            {
                MessageBox.Show($"文件路径错误【{filePath}】");
                return;
            }
            try
            {
                string text = JsonConvert.SerializeObject(jsobj, Formatting.Indented);
                File.WriteAllText(filePath, text);
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.StackTrace);
                return;
            }
            
        }

    }
}
